package com.zzh.rabbit;

import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.util.Log;
import android.view.Menu;

import com.zzh.lib.bar.java.UltimateBarX;
import com.zzh.lib.core.lang.HStringSpannable;
import com.zzh.lib.core.lang.span.TextShapeSpan;
import com.zzh.lib.core.utils.HResUtils;
import com.zzh.lib.core.utils.HTagUtils;
import com.zzh.lib.core.utils.HTaskUtils;
import com.zzh.lib.core.utils.LogUtils;
import com.zzh.mvvm.bus.Messenger;
import com.zzh.rabbit.databinding.ActivitySecondBinding;
import com.zzh.rabbit.ts.RadiusBgSpan;
import com.zzh.rabbit.ts.TextSpan;
import com.zzh.rabbit.ts.TextTagSpan;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class SecondActivity extends AppCompatActivity {
    ActivitySecondBinding bind;
    private int redColor = Color.parseColor("#ff0000");
    private int whiteColor = Color.parseColor("#ffffff");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bind = ActivitySecondBinding.inflate(getLayoutInflater());
        setContentView(bind.getRoot());
        setSupportActionBar(bind.materialToolbar);
        UltimateBarX.statusBar(this).fitWindow(true)
                .color(Color.WHITE).light(true).apply();
        UltimateBarX.navigationBar(this).fitWindow(true).color(Color.BLACK).apply();

        String user1 = getIntent().getStringExtra("user");
        Log.e("---", user1);
        findViewById(R.id.btn).setOnClickListener(v -> {
            User user = new User();
            user.name = "李四";
            Messenger.getDefault().send(user);
            Log.e("-----", "---------发出页面同步消息消息");
        });
        HStringSpannable hss = getHStringSpannable1(RadiusBgSpan.STYLE_FILL);
        bind.mtv.setText(hss);
        bind.mtv2.setText(getHStringSpannable2(RadiusBgSpan.STYLE_STROKE));
        bind.mtv3.setText(getHStringSpannable3());
        bind.mtv4.setText(getHStringSpannable1(RadiusBgSpan.STYLE_FILL));


        initTextSpan1();
        initTextSpan2();
        initTextSpan3();
        initTextSpan4();
    }

    private void initTextSpan1() {
        TextShapeSpan tss = new TextShapeSpan();
        tss.setBackgroundResource(R.drawable.bg_gradient)
                .setPaddingStart(10)
                .setTxtColor(Color.WHITE)
                .setPaddingEnd(10)
                .setMarginBottom(20)
                .setPaddingTop(2).setPaddingBottom(2)
                .setTextSize(HResUtils.sp2px(14))
        ;
        HStringSpannable hss = new HStringSpannable("遂古之初谁传道之上下未行何由考之");
        hss.setSpan(tss, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        bind.mtv5.setText(hss);
    }

    private void initTextSpan2() {
        SpannableString spannableString = new SpannableString("我是哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈");
        TextSpan tagSpan2 = new TextSpan(Paint.Style.FILL, whiteColor, redColor, HResUtils.sp2px(16), 3, 10, 10, 10);
        spannableString.setSpan(tagSpan2, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        bind.mtv6.setText(spannableString);
    }

    private void initTextSpan3() {
        SpannableString spannableString = new SpannableString("我是哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈");
        TextSpan tagSpan2 = new TextSpan(Paint.Style.STROKE, redColor, redColor, HResUtils.sp2px(16), 3, 10, 10, 10, 20, 20);
        spannableString.setSpan(tagSpan2, 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        bind.mtv7.setText(spannableString);
    }

    private void initTextSpan4() {
        HStringSpannable spannableString = new HStringSpannable();
        spannableString.appendFillTag("标5", 15, 20, Color.YELLOW, Color.BLUE, HResUtils.sp2px(8));
        spannableString.appendFillTag("标6", 15, 20, Color.BLACK, Color.BLUE, HResUtils.sp2px(14));
        spannableString.appendFillTag("标7", 15, 20, Color.RED, Color.BLUE, HResUtils.sp2px(18));
        spannableString.appendFillTag("标8", 15, 20, Color.RED, Color.BLUE, HResUtils.sp2px(28));
        spannableString.appendFillTag("标9", 15, 20, Color.RED, Color.BLUE, HResUtils.sp2px(38));
        spannableString.appendFillTag("标1", 15, 20, Color.RED, Color.BLUE, HResUtils.sp2px(48));
        spannableString.appendFillTag("标10", Color.WHITE, 2, R.drawable.bg_gradient);
        spannableString.appendFillTagTextSize("标11", Color.WHITE, HResUtils.sp2px(10), 20, R.drawable.bg_gradient);
        spannableString.append("我是哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈");
        spannableString.setStyle("我是哈", Typeface.BOLD);
        bind.mtv8.setText(spannableString);
        HTaskUtils.getDefaultInstance().execute(() -> {
            runOnUiThread(() -> {
                LogUtils.e("------------: TextView height: " + bind.mtv8.getHeight());
                LogUtils.e("------------: TextView Y: " + bind.mtv8.getY());
                LogUtils.e("------------: TextView X: " + bind.mtv8.getX());

            });
        }, 5000);
    }

    private @NonNull HStringSpannable getHStringSpannable1(int style) {
        HStringSpannable hss = new HStringSpannable("如果我是陈奕迅");
        TextTagSpan tts = new TextTagSpan("陈奕迅", HResUtils.sp2px(18));
        tts.setRadius(10).setTextSize(HResUtils.sp2px(16)).setTextColor(Color.WHITE).setBackgroundColor(Color.RED);


        hss.setSpan(tts, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return hss;
    }

    private @NonNull HStringSpannable getHStringSpannable3() {
        HStringSpannable hss = new HStringSpannable("如果我是陈奕迅");

        TextSpan tts = new TextSpan(Paint.Style.FILL_AND_STROKE, Color.WHITE, Color.RED, HResUtils.sp2px(10), 10);
        hss.setSpan(tts, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return hss;
    }

    private @NonNull HStringSpannable getHStringSpannable2(int style) {
        HStringSpannable hss = new HStringSpannable("如果我是陈奕迅");
        RadiusBgSpan rs = new RadiusBgSpan(Color.RED, Color.BLACK, 5, style);
        hss.setSpan(rs, 4, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return hss;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

}